package com.zjut.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.zjut.domain.Model;
import com.zjut.service.ModelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/model")
public class ModelController {
    @Autowired
    private ModelService modelService;

    //添加试卷模板
    @PostMapping
    public Result save(@RequestBody Model model) {
        if (model.getCalculationScore() == null) model.setCalculationScore(0);
        if (model.getFillScore() == null) model.setFillScore(0);
        if (model.getChoiceScore() == null) model.setChoiceScore(0);
        if (model.getOtherScore() == null) model.setOtherScore(0);
        if (model.getJudgmentScore() == null) model.setJudgmentScore(0);
        if (model.getShortAnswerScore() == null) model.setShortAnswerScore(0);
        if (model.getScore() == null) model.setScore(100);
        //对模板提交表单进行数据校验
        if (model.getCalculationScore() + model.getChoiceScore() + model.getFillScore() + model.getJudgmentScore()
                + model.getShortAnswerScore() + model.getOtherScore() != model.getScore()) {
            return new Result(Code.SAVE_ERR, null, "请输入正确的分值");
        }
        boolean flag = modelService.save(model);
        return new Result(flag ? Code.SAVE_OK : Code.SAVE_ERR, flag);
    }

    //修改试卷模板
    @PutMapping
    public Result update(@RequestBody Model model) {
        if (model.getCalculationScore() + model.getChoiceScore() + model.getFillScore() + model.getJudgmentScore()
                + model.getShortAnswerScore() + model.getOtherScore() != model.getScore()) {
            return new Result(Code.UPDATE_ERR, null, "请输入正确的分值");
        }
        boolean flag = modelService.update(model);
        return new Result(flag ? Code.UPDATE_OK : Code.UPDATE_ERR, flag);
    }

    @PutMapping("/delete")
    public Result delete(@RequestBody Model model) {
        boolean flag = modelService.update(model);
        return new Result(flag ? Code.DELETE_OK : Code.UPDATE_ERR, flag);
    }

    //删除试卷模板
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id) {
        boolean flag = modelService.delete(id);
        return new Result(flag ? Code.DELETE_OK : Code.DELETE_ERR, flag);
    }

    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id) {
        Model model = modelService.getById(id);
        ;
        Integer code = model != null ? Code.GET_OK : Code.GET_ERR;
        String msg = model != null ? "" : "数据查询失败，请重试！";
        return new Result(code, model, msg);
    }

    @GetMapping
    public Result getAll() {
        List<Model> modelList = modelService.getAll();
        Integer code = modelList != null ? Code.GET_OK : Code.GET_ERR;
        String msg = modelList != null ? "" : "数据查询失败，请重试！";
        return new Result(code, modelList, msg);
    }


    //对试卷模型的分页展示
    @GetMapping("/{currentPage}/{pageSize}")
    public Result getPage(@PathVariable int currentPage, @PathVariable int pageSize, Model model) {

        IPage<Model> modelList;
        modelList = modelService.getPage(currentPage, pageSize, model);
        if (currentPage > modelList.getPages()) {
            modelList = modelService.getPage((int) modelList.getPages(), pageSize, model);
        }
        Integer code = modelList != null ? Code.GET_OK : Code.GET_ERR;
        String msg = modelList != null ? "" : "数据查询失败，请重试！";
        return new Result(code, modelList, msg);
    }
}
